package com.gagakuai.executor;

import com.gagakuai.cache.CacheKey;
import com.gagakuai.mapping.BoundSql;
import com.gagakuai.mapping.MappedStatement;
import com.gagakuai.session.ResultHandler;
import com.gagakuai.session.RowBounds;
import com.gagakuai.transcation.Transaction;
import org.apache.commons.logging.Log;

import java.sql.Connection;
import java.sql.SQLException;

/*
 * @program: common-starters
 * @description: 基本执行器
 * @author: houhong
 * @create: 2023-06-29 00:18
 */
public abstract class BaseExecutor implements Executor {

    /*
     * 事务
     */
    protected Transaction transaction;


    private boolean closed;


    @Override
    public Transaction getTransaction() {
        //线程安全问题
        if (closed) {
            throw new ExecutorException("Executor was closed.");
        }
        return transaction;
    }


    @Override
    public boolean isClosed() {
        return closed;
    }

    @Override
    public <E> E query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException {
        return null;


    }

    /*
     * @param parameter:参数
     * @param rowBounds     : 行数
     * @param resultHandler :结果
     * @param key           ：key
     * @param boundSql      ：sql
     * @param ms            ：xml中的方法映射
     * @return {@link E}
     * @Author houhong
     * @Description //TODO
     * @Date 8:54 下午 2023/7/1
     */
    public <E> E queryFromDataBase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) {


        return doQuery(ms, parameter, rowBounds, resultHandler, boundSql);


    }


    public abstract <E> E doQuery(MappedStatement ms, Object obj, RowBounds rs, ResultHandler resultHandler, BoundSql boundSql);


    /*
     * @param log:日志
     * @return {@link Connection}
     * @Author houhong
     * @Description 获取连接
     * @Date 9:12 下午 2023/7/1
     */
    public Connection getConnection(Log log) throws SQLException {

        Connection connection = transaction.getConnection();
        if (log.isDebugEnabled()) {
            //// TODO: 2023/7/1
        }
        return connection;
    }


}